<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../css/animate.css">
    <script src="../js/vue.js"></script>
    <script src="../js/vue-router.js"></script>
    <script src="http://47.104.209.44/base/js/jquery-1.10.1.min.js"></script>
    <title>Document</title>
    <style>
         .box{
            width:50%;
            min-height:300px;
            border:1px solid #999;
            border-radius: 10px;
            background: rgb(228, 174, 183);
            padding: 30px;

        }
        .router-link-exact-active, .router-link-active{
            font-size: 30px;
            font-weight: bold;
            color:#f50;
        }
    </style>
</head>
<body>
    <div id="app">
        <h2>{{title}} </h2>

        <!-- 5.路由出口 路由匹配到的组件将渲染在这里 -->
       <router-view class="box"></router-view>

        <hr>
        <!-- 6.路由跳转 声明式 -->
        <router-link to="/home/wh2114-daydayup">首页</router-link>
        <router-link to="/list">列表</router-link>
        <router-link :to="{name:'mine',query:{mid:2114,name:'TongTong'}}">我的</router-link>
        <router-link to="/notfound">404</router-link>

       

      

    </div>

    <template id="home">
        <div>
            <h2>Home - Home - Home </h2>
            <h2>token --- {{$route.params.token}}</h2>
            <button @click="gotomine">点击进入我的</button>
            <button @click="goback">返回上一级</button>
        </div>
    </template>
    <template id="list">
        <div>
            <h3>list - list - list  </h3>
            <button @click="$router.back()">返回上一级</button>
            <div>
                <p v-for="(v,i) in list" :key="i">
                    <router-link :to="{name:'detail',query:v}">{{v.name}} --- {{i}} </router-link>

                </p>
            </div>
        </div>
    </template>
    <template id="detail">
        <div>
            <h2>Home - Home - Home </h2>
            <h2>歌单详情 - detail - <button @click="$router.back()">返回</button></h2>
            <h2>name -- {{$route.query.name}}</h2>
            <h2>歌曲数量 -- {{$route.query.resourceCount}}</h2>

        </div>
    </template>
    <template id="mine">
        <div>
            <h2>Mine - Mine - 个人中心</h2>
            <h2>mid --- {{$route.query.mid}}</h2>
            <h2>name --- {{$route.query.name}}</h2>
            <button @click="gotohome">返回</button>
        </div>
    </template>
    <template id="notfound">
        <div>
            <h2>notfound - notfound - 404 </h2>
            <button @click="gotohome2">返回首页</button>
        </div>
    </template>

  
    <script>

        // 1.定义路由组件
       const Home = {
           template:"#home",
           
           methods:{
               gotomine(){
                   router.push({name:'mine',query:{mid:2114,name:'瞳瞳'}})
               },
               goback(){
                   //route.go(-1)
                   this.$router.back()
               }
           }
       }
      
       const Detail = {
           template:'#detail'
       }
       const List = {
           template:"#list",
           data(){
               return{
                   list:[]
               }
           },
           mounted(){
               $.ajax({
                   url:"http://47.104.209.44:3333/playlist/catlist",
                   type:"GET",
                   data:{},
                   success:res=>{
                       console.log(res);
                       this.list = res.sub
                   }
               })
           }
       }

       const Mine = {
           template:"#mine",
           methods:{
            gotohome(){
                router.push({name:'home',params:{token:'wh2114-DayDayUp'}
                    })
            }
           }

           
       }
       const NotFound = {
           template:"#notfound",
            methods:{
            gotohome2(){
                router.push({name:'home',params:{token:'wh2114-DayDayUp'}
                    })
            }
           
       }
       }
           

        // 2.定义一些路由
        //每个路由都需要映射一个组件
        const routes = [
            {path:"/",redirect:"/home"},
            {path:"/home/:token",component:Home,name:'home'},
            {path:"/list",alist:"/goods",component:List,name:'list'},
            {path:"/detail",component:Detail,name:'detail'},
            {path:"/mine",component:Mine,name:'mine'},
            {path:"/404",component:NotFound,name:'notfound'},
            {path:"*",redirect:"/404"},
        ]
        // 3. 创建路由实例并传递 `routes` 配置 你可以在这里输入更多的配置
        //# 表示 hash 路由
        const router = new VueRouter({
            routes,
            mode:'hash',
            base:'',
            scrollBehavior(to,from,savePosition){
                return savePosition
            }
        })


        // scrollBehavior 滚动行为 
        // base 应用的基路径  一般不用设置   采用是相对路径   上线的时候才需要单独去设置路径
        // mode 路由模式   
        // hash  url地址的 hash 模式  兼容性强     带#号 可以刷新浏览器  但是地址比较长 不简洁
        // history  不带 # 号  不能刷新浏览器 (需要服务器重定向404 )   比较简洁优雅   依赖 HTML5 History API 和服务器配置

        // 4.路由挂载到根实例
        const vm = new Vue ({
            router, // this.$route
            data:{
                title:"Vue 路由初步 - 跳转"
            },
            watch:{
                '$route':function(to,from){
                    console.log(to);
                    console.log(from);
                }
            },
            mounted(){
                document.getElementsByTagName('title')[0].innerHTML = this.title
                console.log(this.$router);
                 // 路由对象  负责路由跳转 路由导航和路由全局监听
                console.log(this.$route);
                // 路由信息对象  当前激活的路由对象  路由参数 query(?)&params(:)  
            }
        }).$mount('#app')
    </script>

        <!-- router.go(1)
        router.goforward();    前进
        router.go(-1);       后退 
        router.back()   -->
</body>
</html>